package cool.taomu.toolkit.utils

import com.google.inject.Inject
import javax.mail.Message
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.MimeMessage
import javax.mail.internet.InternetAddress
import org.slf4j.LoggerFactory

class MailUtils {

    val static LOG = LoggerFactory.getLogger(MailUtils);

    @Inject
    Session session;

    def boolean sendTextEmail(String from, String[] tos, String subject, String content) {
        try {
            var message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, tos.join(";"));
            message.setSubject(subject);
            message.setText(content);

            Transport.send(message);
            LOG.info("邮件发送成功");
            return true;
        } catch (MessagingException e) {
            LOG.info(" 邮件发送失败: " + e.getMessage());
            return false;
        }
    }
}
